package com.lvmama.pangolin.timer.processor;




import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.JSONUtil;
import com.lvmama.pangolin.common.MD5Util;
import com.lvmama.pangolin.entity.lmm.LMMCareProduct;
import com.lvmama.pangolin.lmm.service.LMMCareProductService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.HttpSignUtil;
import com.lvmama.pangolin.util.HttpsUtil;
import com.lvmama.pangolin.util.SpringContextUtil;


public class SynCareProduct extends CommonJob {

	@Autowired
	private LMMCareProductService careProductService;
	
	private String url = "allin.lvmama.com/pangolin/syn/getNewProd?";
	
	private Logger logger = Logger.getLogger(SynCareProduct.class);
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		try
		{
			
			if(careProductService == null)
			{
				careProductService = (LMMCareProductService) SpringContextUtil.getBean(LMMCareProductService.class);
			}
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("limit", 1);
			List<LMMCareProduct> list = careProductService.queryProductbyTime(param);
			Date date = new Date();
			long time = date.getTime();
			if(list != null && !list.isEmpty())
			{
				Date lastDate = list.get(0).getUpdateTime();
				if(date.compareTo(lastDate) < 0)
				{
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					cal.set(Calendar.HOUR_OF_DAY, 0);
					cal.set(Calendar.MINUTE, 0);
					cal.set(Calendar.MILLISECOND, 0);
					time = cal.getTimeInMillis();
				}
				else
				{
					time = lastDate.getTime();
				}
			}
			String key = MD5Util.encode(time+"");
			url = ConfigManager.getInstance().getParamValue(parameter, "URL", url);
			List<LMMCareProduct> careList = transferCareProd(key,time+"",url);
			if(careList != null && !careList.isEmpty())
			{
				careProductService.delAndInsert(careList);
				
			}
			
		}catch (Exception e)
		{
			logger.error("更新任务状态失败!",e);
		}
		
	}
	
	private  List<LMMCareProduct> transferCareProd(String authKey,String time,String url) throws Exception
	{
		List<LMMCareProduct> careList = new ArrayList<LMMCareProduct>();
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("key", authKey);
		param.put("time", time);
		String sign = HttpSignUtil.getSignFromMap( param);
		String json = HttpsUtil.requestGet(url + sign);
		System.out.println(json);
		JSONObject jo = JSONUtil.getObject(json);
		if(jo!= null && "0".equals(jo.getString("code")))
		{
			
			JSONArray jsonArr = jo.getJSONArray("data");
			if(jsonArr != null && jsonArr.size() > 0)
			{
				JSONObject item = null;
				LMMCareProduct care = null;
				for(int i=0;i<jsonArr.size();i++)
				{
					item = jsonArr.getJSONObject(i);
					if(item == null)
						continue;
					care = new LMMCareProduct();
					care.setActualName(item.getString("actualName"));
					care.setBusinessCode(item.getString("businessCode"));
					care.setCityCode(item.getString("cityCode"));
					care.setCityName(item.getString("cityName"));
					care.setDealFlag(Integer.parseInt(item.getString("dealFlag")));
					care.setInsertTime(new Date(Long.valueOf(item.getString("insertTime"))));
					care.setProductId(item.getString("productId"));
					care.setProductName(item.getString("productName"));
					care.setQnr_name(item.getString("qnr_name"));
					care.setQnr_url(item.getString("qnr_url"));
					care.setTc_name(item.getString("tc_name"));
					care.setTc_url(item.getString("tc_url"));
					care.setTn_name(item.getString("tn_name"));
					care.setTn_url(item.getString("tn_url"));
					care.setUpdateTime(new Date(Long.valueOf(item.getString("updateTime"))));
					care.setXc_name(item.getString("xc_name"));
					care.setXc_url(item.getString("xc_url"));
					care.setYl_name(item.getString("yl_name"));
					care.setYl_url(item.getString("yl_url"));
					
					careList.add(care);
							
				}
			}
			
		}
		
		return careList;
		
	}

}
